package com.aspirecn.core.datacache.useragent;

import jodd.cache.Cache;
import jodd.cache.LRUCache;

import com.aspire.core.match.ua.object.Device;

/**
 * 基础缓存服务，专门为ua适配，本地是jodd缓存，远程是memcached
 * @author wangxinning
 *
 */
public class UserAgentCommonCache {

	//本地缓存
	private final static int CACHE_SIZE = 10000;
	
	//jodd本地缓存
	public static Cache<String,Device> localCacheService = new LRUCache<String,Device>(CACHE_SIZE);
	
	private static Device nullDevice = null;	
	public final static int NULL_DEVICE_ID = -999;
	
	public static void addToCache( String ua, Device device )
	{
		String key = getCacheKey(ua);
		
		if( device == null )
		{
			//降低内存消耗
			if( nullDevice == null )
			{
				nullDevice = new Device();
				nullDevice.setDevice_id(NULL_DEVICE_ID);
			}
			
			localCacheService.put(key, nullDevice);
		}
		else
		{
			localCacheService.put(key, device);
		}
	}
	
	public static Device getFromCache( String ua )
	{
		String key = getCacheKey(ua);
		
		Device device = localCacheService.get(key);
		return device;	
	}
	
	private static String getCacheKey( String ua )
	{
		return "sharecommon.ua." + ua;
	}
}
